package com.example.mq2.mqserver.datacenter;

import com.example.mq2.Mq2Application;
import com.example.mq2.mqserver.core.Binding;
import com.example.mq2.mqserver.core.Exchange;
import com.example.mq2.mqserver.core.ExchangeType;
import com.example.mq2.mqserver.core.MSGQueue;
import com.example.mq2.mqserver.mapper.MqMapper;

import java.io.File;
import java.util.List;

/**
 * 管理数据库相关操作
 */
public class DataBaseManager {

    //不使用 Spring 管理依赖
    private MqMapper mqMapper;

    /**
     * 初始化操作
     * 建库建表，另外 RabbitMQ 还有一个额外的设定，会有一个匿名的交换机(直接交换机)
     */
    public void init() {
        //手动获取 Bean 对象
        mqMapper = Mq2Application.context.getBean(MqMapper.class);
        if(!existsDB()) {
            //不存在才创建
            File dirFile = new File("./data");
            dirFile.mkdirs();
            //建库
            createDB();
            //创建匿名交换机
            createDefaultInfo();
            System.out.println("[DataBaseManager] 数据库数据初始化完毕！");
        } else {
            System.out.println("[DataBaseManager] 数据库数据已经存在！");
        }
    }

    /**
     * 创建默认信息
     */
    private void createDefaultInfo() {
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.Direct);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        mqMapper.insertExchange(exchange);
    }

    /**
     * 创建数据库
     */
    private void createDB() {
        mqMapper.createExchangeTable();
        mqMapper.createQueueTable();
        mqMapper.createBindingTable();
    }

    private boolean existsDB() {
        File dbFile = new File("./data/meta.db");
        return dbFile.exists();
    }

    /**
     * 删除数据库文件
     */
    public void deleteDB() {
        if(!existsDB()) {
            System.out.println("[DataBaseManager] 删除数据库文件时，发现文件不存在！");
            return;
        }
        File dataFile = new File("./data/meta.db");
        boolean ok1 = dataFile.delete();
        if(!ok1) {
            System.out.println("[DataBaseManager] 删除数据库文件失败！");
        }
        File dirFile = new File("./data");
        boolean ok2 = dirFile.delete();
        if (!ok2) {
            System.out.println("[DataBaseManager] 删除数据库目录失败！");
        }
        if(ok1 && ok2) {
            System.out.println("DataBaseManager] 数据库删除成功！");
        }
    }


    public void insertExchange(Exchange exchange) {
        mqMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName) {
        mqMapper.deleteExchange(exchangeName);
    }

    public List<Exchange> selectExchangeAll() {
        return mqMapper.selectExchangeAll();
    }

    public void insertQueue(MSGQueue queue) {
        mqMapper.insertQueue(queue);
    }

    public void deleteQueue(String queueName) {
        mqMapper.deleteQueue(queueName);
    }

    public List<MSGQueue> selectQueueAll() {
        return mqMapper.selectQueueAll();
    }

    public void insertBinding(Binding binding) {
        mqMapper.insertBinding(binding);
    }

    public void deleteBinding(Binding binding) {
        mqMapper.deleteBinding(binding);
    }

    public List<Binding> selectBindingAll() {
        return mqMapper.selectBindingAll();
    }

}
